package com.zhihuichongcheng.application.ui.cricle.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.dialog.DialogOnClick;
import com.smartlib.cmnObject.util.DateUtil;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.cricle.CommentBean;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 3water on 2016/10/28.
 */

public class CommentAdapter extends BaseAdapter {
    private Context mContext;
    private List<CommentBean> mListData = new ArrayList<>();
    private DialogOnClick<CommentBean> onClick;

    public CommentAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<CommentBean> list) {
        this.mListData = list;
    }


    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold viewHold;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listadapter_cricle_comment, null);
            viewHold = new ViewHold();
            viewHold.mTvTime = (TextView) convertView.findViewById(R.id.tv_time);
            viewHold.mTvContent = (TextView) convertView.findViewById(R.id.tv_content);
            viewHold.mImAvatar = (ImageView) convertView.findViewById(R.id.im_avatar);
            viewHold.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            viewHold.mViewBottom = (View) convertView.findViewById(R.id.view_bottom);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();
        }
        final CommentBean bean = mListData.get(position);
        Date date = DateUtil.parseStringToDate(bean.getCreateTime(), "yyyy年MM月dd日 HH:mm:ss");
        viewHold.mTvTime.setText(DateUtil.format(date));
        UserBean userBean = bean.getUser();
        String content = bean.getContent();
        if (TextUtils.isEmpty(content)) {
            content = "null";
        }
        viewHold.mTvContent.setText(content);
        if (userBean != null) {
            viewHold.mTvName.setText(userBean.getUserName());
            Glide.with(mContext).load(userBean.getLogoUrl()).placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(viewHold.mImAvatar);
        } else {
            viewHold.mTvName.setText("");
            Glide.with(mContext).load("").placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(viewHold.mImAvatar);
        }
        viewHold.mImAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null) {
                    onClick.onClick(bean);
                }
            }
        });
        if (position == mListData.size() - 1) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) viewHold.mViewBottom.getLayoutParams();
            params.setMargins(DeviceUtil.dip2px(mContext, 0), 0, 0, 0);
            viewHold.mViewBottom.setLayoutParams(params);
        } else {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) viewHold.mViewBottom.getLayoutParams();
            params.setMargins(DeviceUtil.dip2px(mContext, 60), 0, 0, 0);
            viewHold.mViewBottom.setLayoutParams(params);
        }
        return convertView;
    }

    public void addItem(CommentBean commentBean) {
        mListData.add(0, commentBean);
    }

    public void setOnClick(DialogOnClick<CommentBean> onClick) {
        this.onClick = onClick;
    }

    class ViewHold {
        private ImageView mImAvatar;
        private TextView mTvName, mTvContent, mTvTime;
        private View mViewBottom;
    }
}
